/*  ------------------------------------------------------------------------
	 NQ Common Services Library
	 
	 Homepage: http://www.awzhome.de/
	 ------------------------------------------------------------------------
	 
	 The contents of this file are subject to the Mozilla Public License
	 Version 1.1 (the "License"); you may not use this file except in
	 compliance with the License. You may obtain a copy of the License at
	 http://www.mozilla.org/MPL/
	 
	 Software distributed under the License is distributed on an "AS IS"
	 basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	 License for the specific language governing rights and limitations
	 under the License.
	
	 The Original Code is code of NQ Common Services Library.

	 The Initial Developer of the Original Code is Andreas Weizel.
	 Portions created by the Initial Developer are
	 Copyright (C) 2006-2012 Andreas Weizel. All Rights Reserved.
	 
	 Contributor(s): (none)
	 
	 Alternatively, the contents of this file may be used under the terms
	 of the GNU General Public License Version 2.0 or later (the "GPL"),
	 or the GNU Lesser General Public License Version 2.1 or later (the
	 "LGPL"), in which case the provisions of the GPL or the LGPL are
	 applicable instead of those above. If you wish to allow use of your
	 version of this file only under the terms of the GPL or the LGPL and
	 not to allow others to use your version of this file under the MPL,
	 indicate your decision by deleting the provisions above and replace
	 them with the notice and other provisions required by the GPL or the
	 LGPL. If you do not delete the provisions above, a recipient may use
	 your version of this file under the terms of any of the MPL, the GPL
	 or the LGPL.
	 ------------------------------------------------------------------------
*/

using System;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Media;
using System.Windows.Input;
using AWZhome.NQ.Core;
using AWZhome.NQ.Core.Fluent;
using System.Windows.Controls.Primitives;

namespace AWZhome.NQ.CommonServices
{

	internal class NQBarItem : IBarItem
	{
		private BarService _parentBarServ = null;
		private Dictionary<string, NQBarItem> _itemList = new Dictionary<string, NQBarItem>();
		private Control _itemControl = null;
		private Image _buttonImage = null;
		private TextBlock _buttonTextBlock = null;
		private string _itemKey = null;
		private BarItemType _itemType;
		private bool _hideAlways = false;
		private bool _itemVisible = true;
		private ImageSource _iconTemp = null;
		private string _headerTemp = null;
		private bool _showText = false;
		private RoutedCommand _routedCommand;

		internal NQBarItem(BarService parentBarServ, BarItemType itemType, string itemKey)
		{
			this._parentBarServ = parentBarServ;
			this._itemType = itemType;
			this._itemKey = itemKey;

			switch (itemType)
			{
				case BarItemType.Button:
					// Create a new button
					Button buttonItem = new Button();
					buttonItem.Content = this.CreateButtonInnerContent();
					buttonItem.Height = 22;
					buttonItem.MinWidth = 22;
					_itemControl = buttonItem;
					this.ShowImage = true;
					this.ShowText = false;
					buttonItem.Click += new RoutedEventHandler(buttonItem_Click);
					break;

				case BarItemType.CheckBox:
					// Create a new button
					CheckBox checkBoxItem = new CheckBox();
					checkBoxItem.Content = this.CreateButtonInnerContent();
					checkBoxItem.Height = 22;
					checkBoxItem.MinWidth = 22;
					_itemControl = checkBoxItem;
					this.ShowImage = true;
					this.ShowText = false;
					checkBoxItem.Click += new RoutedEventHandler(buttonItem_Click);
					break;

				case BarItemType.RadioButton:
					// Create a new button
					RadioButton radioButtonItem = new RadioButton();
					radioButtonItem.Content = this.CreateButtonInnerContent();
					radioButtonItem.Height = 22;
					radioButtonItem.MinWidth = 22;
					_itemControl = radioButtonItem;
					this.ShowImage = true;
					this.ShowText = false;
					radioButtonItem.Click += new RoutedEventHandler(buttonItem_Click);
					break;

				case BarItemType.DropDownButton:
				case BarItemType.SplitButton:

					// Create a drop-down button as a menu
					//DropDownButton newDropButton = new DropDownButton();
					//newDropButton.Content = this.CreateButtonInnerContent();
					//newDropButton.DropDown = new ContextMenu();
					//_itemControl = newDropButton;
					//this.ShowImage = true;
					//this.ShowText = false;
					//newDropButton.Click += new RoutedEventHandler(newDropButton_Click);

					Menu dropMenu = new Menu();

					dropMenu.Background = Brushes.Transparent;
					MenuItem dropMenuItem = new MenuItem();
					dropMenuItem.Header = this.CreateDropDownButtonHeader();
					dropMenuItem.Height = 22;
					dropMenuItem.MinWidth = 22;

					Thickness itemPadding = dropMenuItem.Padding;
					itemPadding.Right = 1;
					itemPadding.Left = 2;
					itemPadding.Top -= 2;
					itemPadding.Bottom--;
					dropMenuItem.Padding = itemPadding;

					//dropMenuItem.Width = Double.NaN;
					//dropMenuItem.Height = Double.NaN;

					dropMenu.Items.Add(dropMenuItem);
					_itemControl = dropMenu;

					this.ShowImage = true;
					this.ShowText = false;
					break;

				// Create a split button
				//DropDownButton newSplit = new DropDownButton();
				//newSplit.Content = this.CreateButtonInnerContent();
				//newSplit.DropDown = new ContextMenu();
				//_itemControl = newSplit;
				//this.ShowImage = true;
				//this.ShowText = false;
				//newSplit.Click += new RoutedEventHandler(newDropButton_Click);

				//Menu splitMenu = new Menu();
				//splitMenu.Background = Brushes.Transparent;
				//MenuItem splitMenuItem = new MenuItem();
				//splitMenu.Items.Add(splitMenuItem);
				//_itemControl = splitMenu;
				//break;

				case BarItemType.MenuItem:
					// Create a menu item
					MenuItem newMenuItem = new MenuItem();
					newMenuItem.MinHeight = 20;
					newMenuItem.MinWidth = 20;

					_buttonImage = new Image();
					_buttonImage.Width = 16;
					_buttonImage.Height = 16;
					_itemControl = newMenuItem;
					this.ShowText = true;
					this.ShowImage = true;
					//ToolBar.SetOverflowMode(_itemControl, OverflowMode.Always);
					newMenuItem.Click += new RoutedEventHandler(newMenuItem_Click);

					// Create a CommandBinding in the root window
					_routedCommand = new RoutedCommand();
					CommandBinding commandBinding = new CommandBinding(_routedCommand);
					commandBinding.Executed +=
						delegate(object sender, ExecutedRoutedEventArgs e)
						{
							this.RaiseItemClickEvent();
						};
					commandBinding.CanExecute +=
						delegate(object sender, CanExecuteRoutedEventArgs e)
						{
							e.CanExecute = true;
						};
					Window rootWindow = Window.GetWindow(parentBarServ.BarHost);
					if (rootWindow != null)
					{
						rootWindow.CommandBindings.Add(commandBinding);
					}
					//newMenuItem.Command = _routedCommand;

					break;

				case BarItemType.Label:
					// Create a label
					Label newLabel = new Label();
					_itemControl = newLabel;
					break;

				case BarItemType.ComboBox:
					// Create a combo box
					ComboBox newComboBox = new ComboBox();
					_itemControl = newComboBox;
					break;

				case BarItemType.TextBox:
					// Create a text box
					TextBox newTextBox = new TextBox();
					_itemControl = newTextBox;
					break;

				case BarItemType.Separator:
					// Create a separator
					Separator newSeparator = new Separator();
					_itemControl = newSeparator;
					break;
			}

			// Newer show in overflow menu
			ToolBar.SetOverflowMode(_itemControl, OverflowMode.Never);
		}

		#region INQBarItem items

		public bool ItemExists(string itemKey)
		{
			if (itemKey != null)
			{
				return _itemList.ContainsKey(itemKey);
			}
			else
			{
				return false;
			}
		}

		public string[] GetItemKeys()
		{
			string[] items = new string[_itemList.Keys.Count];
			_itemList.Keys.CopyTo(items, 0);
			return items;
		}

		public string ItemText
		{
			get
			{
				if ((_itemControl is Button)
					|| (_itemControl is CheckBox)
					|| (_itemControl is RadioButton)
					|| (_itemControl is Menu))
				{
					return _buttonTextBlock.Text;
				}
				//else if (_itemControl is Menu)
				//{
				//  //return (string) ((MenuItem) ((Menu) _itemControl).Items[0]).Header;
				//  return _headerTemp;
				//}
				else if (_itemControl is MenuItem)
				{
					//return (string) ((MenuItem) _itemControl).Header;
					return _headerTemp;
				}
				else if (_itemControl is TextBox)
				{
					return ((TextBox) _itemControl).Text as string;
				}
				else if (_itemControl is Label)
				{
					return ((Label) _itemControl).Content as string;
				}
				else
				{
					return null;
				}
			}
			set
			{
				if ((_itemControl is Button)
					|| (_itemControl is CheckBox)
					|| (_itemControl is RadioButton)
					|| (_itemControl is Menu))
				{
					// Filter out shortcut underscores
					_buttonTextBlock.Text = this.FilterShortcutChars(value);
				}
				//else if (_itemControl is Menu)
				//{
				//  if (_showText)
				//  {
				//    ((MenuItem) ((Menu) _itemControl).Items[0]).Header = value;
				//  }
				//  _headerTemp = value;
				//}
				else if (_itemControl is MenuItem)
				{
					if (_showText)
					{
						((MenuItem) _itemControl).Header = value;
					}
					_headerTemp = value;
				}
				else if (_itemControl is TextBox)
				{
					((TextBox) _itemControl).Text = value;
				}
				else if (_itemControl is Label)
				{
					((Label) _itemControl).Content = value;
				}
			}
		}

		public ImageSource ItemImage
		{
			get
			{
				if ((_itemControl is Button)
					|| (_itemControl is CheckBox)
					|| (_itemControl is RadioButton)
					|| (_itemControl is Menu)
					|| (_itemControl is MenuItem))
				{
					return _buttonImage.Source;
				}
				//else if (_itemControl is Menu)
				//{
				//  //return (ImageSource) ((MenuItem) ((Menu) _itemControl).Items[0]).Icon;
				//  return _iconTemp;
				//}
				//else if (_itemControl is MenuItem)
				//{
				//  //return (ImageSource) ((MenuItem) _itemControl).Icon;
				//  return _iconTemp;
				//}
				else
				{
					return null;
				}
			}
			set
			{
				if ((_itemControl is Button)
					|| (_itemControl is CheckBox)
					|| (_itemControl is RadioButton)
					|| (_itemControl is Menu)
					|| (_itemControl is MenuItem))
				{
					_buttonImage.Source = value;
					_iconTemp = value;
				}


				//else if (_itemControl is Menu)
				//{
				//  if (_showImage)
				//  {
				//    ((MenuItem) ((Menu) _itemControl).Items[0]).Icon = value;
				//  }
				//  _iconTemp = value;
				//}
				//else if (_itemControl is MenuItem)
				//{
				//  if (_showImage)
				//  {
				//    ((MenuItem) _itemControl).Icon = value;
				//  }
				//  _iconTemp = value;
				//}
			}
		}

		public string ItemKey
		{
			get
			{
				return _itemKey;
			}
		}

		public string ItemToolTip
		{
			get
			{
				return (string) _itemControl.ToolTip;
			}
			set
			{
				_itemControl.ToolTip = this.FilterShortcutChars(value);
			}
		}

		public bool ItemEnabled
		{
			get
			{
				if (_itemControl is Menu)
				{
					return ((MenuItem) ((Menu) _itemControl).Items[0]).IsEnabled;
				}
				else
				{
					return _itemControl.IsEnabled;
				}
			}
			set
			{
				if (_itemControl is Menu)
				{
					((MenuItem) ((Menu) _itemControl).Items[0]).IsEnabled = value;
				}
				else
				{
					_itemControl.IsEnabled = value;

					// Also set opacity of image, if one is set
					if (_buttonImage != null)
					{
						_buttonImage.Opacity = value ? 1 : 0.4;
					}
				}
			}
		}

		public BarItemType ItemType
		{
			get
			{
				return _itemType;
			}
			set
			{
				_itemType = value;
			}
		}

		public bool ItemVisible
		{
			get
			{
				return _itemVisible;
			}
			set
			{
				_itemVisible = value;
				_itemControl.Visibility = (value && !_hideAlways) ? Visibility.Visible : Visibility.Collapsed;
			}
		}

		public double ItemSize
		{
			get
			{
				if (_itemControl is TextBox)
				{
					return _itemControl.Width;
				}
				return Double.NaN;
			}
			set
			{
				if (_itemControl is TextBox)
				{
					_itemControl.Width = value;
				}
			}
		}

		public bool HideAlways
		{
			get
			{
				return _hideAlways;
			}
			set
			{
				_hideAlways = value;
				// Update item visibility
				this.ItemVisible = this.ItemVisible;
			}
		}

		public bool ItemChecked
		{
			get
			{
				if (_itemControl is MenuItem)
				{
					return ((MenuItem) _itemControl).IsChecked;
				}
				else if (_itemControl is ToggleButton)
				{
					return ((ToggleButton) _itemControl).IsChecked.Value;
				}
				else
				{
					throw new InvalidOperationException("Property not supported for this type of bar item.");
				}
			}
			set
			{
				if (_itemControl is MenuItem)
				{
					((MenuItem) _itemControl).IsChecked = value;
				}
				else if (_itemControl is ToggleButton)
				{
					((ToggleButton) _itemControl).IsChecked = value;
				}
				else
				{
					throw new InvalidOperationException("Property not supported for this type of bar item.");
				}
			}
		}

		public bool ShowImage
		{
			get
			{
				switch (_itemType)
				{
					case BarItemType.Button:
					case BarItemType.DropDownButton:
					case BarItemType.SplitButton:
					case BarItemType.CheckBox:
					case BarItemType.RadioButton:
						return (_buttonImage.Visibility == Visibility.Visible);

					case BarItemType.MenuItem:
						return (((MenuItem) _itemControl).Icon != null);
					//return _showImage;

					default:
						return false;
				}
			}
			set
			{
				switch (_itemType)
				{
					case BarItemType.Button:
					case BarItemType.DropDownButton:
					case BarItemType.SplitButton:
					case BarItemType.CheckBox:
					case BarItemType.RadioButton:
						if (value)
						{
							_buttonImage.Visibility = Visibility.Visible;
						}
						else
						{
							_buttonImage.Visibility = Visibility.Collapsed;
						}
						break;

					case BarItemType.MenuItem:
						if (value)
						{
							((MenuItem) _itemControl).Icon = _buttonImage;
						}
						else
						{
							((MenuItem) _itemControl).Icon = null;
						}
						_buttonImage.Visibility = Visibility.Visible;
						break;

					//_showImage = value;
					//if (value)
					//{
					//  this.ItemImage = _iconTemp;
					//}
					//else
					//{
					//  this.ItemImage = null;
					//}
					//break;
				}

			}
		}

		public bool ShowText
		{
			get
			{
				switch (_itemType)
				{
					case BarItemType.Button:
					case BarItemType.DropDownButton:
					case BarItemType.SplitButton:
					case BarItemType.CheckBox:
					case BarItemType.RadioButton:
						return (_buttonTextBlock.Visibility == Visibility.Visible);

					case BarItemType.MenuItem:
						//case NQBarItemType.DropDownButton:
						//case NQBarItemType.SplitButton:
						return _showText;

					default:
						return false;
				}
			}
			set
			{
				switch (_itemType)
				{
					case BarItemType.Button:
					case BarItemType.DropDownButton:
					case BarItemType.SplitButton:
					case BarItemType.CheckBox:
					case BarItemType.RadioButton:
						if (value)
						{
							_buttonTextBlock.Visibility = Visibility.Visible;
						}
						else
						{
							_buttonTextBlock.Visibility = Visibility.Collapsed;
						}
						break;

					case BarItemType.MenuItem:
						//case NQBarItemType.DropDownButton:
						//case NQBarItemType.SplitButton:
						_showText = value;
						if (value)
						{
							this.ItemText = _headerTemp;
						}
						else
						{
							this.ItemText = null;
						}
						break;
				}
			}
		}

		public bool RightAlign
		{
			get
			{
				return false;
			}
			set
			{
				// This is not implemented by now
			}
		}

		public int SelectedListIndex
		{
			get
			{
				if (_itemControl is ComboBox)
				{
					return ((ComboBox) _itemControl).SelectedIndex;
				}
				else
				{
					throw new InvalidOperationException("SelectedListIndex is only available for ComboBox items.");
				}
			}
			set
			{
				if (_itemControl is ComboBox)
				{
					((ComboBox) _itemControl).SelectedIndex = value;
				}
			}
		}

		public string Keys
		{
			get
			{
				if (_itemControl is MenuItem)
				{
					return ((MenuItem) _itemControl).InputGestureText;
				}
				else
				{
					return null;
				}
			}
			set
			{
				if (_itemControl is MenuItem)
				{
					MenuItem menuItem = (MenuItem) _itemControl;

					// Set KeyGesture for the MenuItem's command
					_routedCommand.InputGestures.Clear();
					if (!String.IsNullOrEmpty(value))
					{
						// We get the gesture text for the control key (usually "Ctrl" in English) from resource file
						IResourceManager servRes = ServiceCore
							.Get<Services.IResourceService>()
							.To;
						menuItem.InputGestureText = value.Replace("Control", servRes.GetResString("NQCS", "GeneralMenu.ControlKeyText"));
						servRes = null;
						KeyGestureConverter keyGestureConverter = new KeyGestureConverter();
						_routedCommand.InputGestures.Add((KeyGesture) keyGestureConverter.ConvertFromString(value));
					}
					else
					{
						menuItem.InputGestureText = "";
					}
				}
			}
		}

		public void AddItem(string itemKey)
		{
			if (
						_parentBarServ.ItemExists(itemKey) && !this.ItemExists(itemKey) &&
						((_itemType == BarItemType.MenuItem) ||
							(_itemType == BarItemType.DropDownButton) ||
							(_itemType == BarItemType.SplitButton))
					)
			{
				NQBarItem item = _parentBarServ._GetItem(itemKey);
				// Add item as submenu entry
				_itemList.Add(itemKey, item);
				if (_itemControl is MenuItem)
				{
					((MenuItem) _itemControl).Items.Add(item.ItemControl);
				}
				else if (_itemControl is Menu)
				{
					((MenuItem) ((Menu) _itemControl).Items[0]).Items.Add(item.ItemControl);
				}
			}
			else if (_itemType == BarItemType.ComboBox)
			{
				// Add string item to combo box
				((ComboBox) _itemControl).Items.Add(itemKey);
			}
		}

		public void InsertItem(string itemKey, string neighborKey, bool insertBefore)
		{
			if (
					_parentBarServ.ItemExists(itemKey) && ((neighborKey == null) || _parentBarServ.ItemExists(neighborKey)) &&
					!this.ItemExists(itemKey) &&
					((_itemType == BarItemType.MenuItem) ||
						(_itemType == BarItemType.DropDownButton) ||
						(_itemType == BarItemType.SplitButton))
				)
			{
				NQBarItem item = _parentBarServ._GetItem(itemKey);
				NQBarItem neighborItem = null;
				int newindex = 0;
				if (neighborKey == null)
				{
					newindex = 0;
				}
				else
				{
					neighborItem = _parentBarServ._GetItem(neighborKey);
					if (_itemControl is MenuItem)
					{
						newindex = ((MenuItem) _itemControl).Items.IndexOf(neighborItem.ItemControl) + (insertBefore ? 0 : 1);
					}
					else if (_itemControl is Menu)
					{
						newindex = ((MenuItem) ((Menu) _itemControl).Items[0]).Items.IndexOf(neighborItem.ItemControl) + (insertBefore ? 0 : 1);
					}
				}
				// Add item as submenu entry before/after another specified entry
				_itemList.Add(itemKey, item);
				if (_itemControl is MenuItem)
				{
					((MenuItem) _itemControl).Items.Insert(newindex, item.ItemControl);
				}
				else if (_itemControl is Menu)
				{
					((MenuItem) ((Menu) _itemControl).Items[0]).Items.Insert(newindex, item.ItemControl);
				}
			}
			else if (_itemType == BarItemType.ComboBox)
			{
				// Insert a string item after another item in the combo box
				ComboBox comboBox = (ComboBox) _itemControl;
				comboBox.Items.Insert(comboBox.Items.IndexOf(neighborKey), itemKey);
			}
		}

		public void RemoveItem(string itemKey)
		{
			if (
					_parentBarServ.ItemExists(itemKey) && this.ItemExists(itemKey) &&
					((_itemType == BarItemType.MenuItem) ||
						(_itemType == BarItemType.DropDownButton) ||
						(_itemType == BarItemType.SplitButton))
				)
			{
				NQBarItem item = _parentBarServ._GetItem(itemKey);
				// Add item as submenu entry
				_itemList.Remove(itemKey);
				if (_itemControl is MenuItem)
				{
					((MenuItem) _itemControl).Items.Remove(item.ItemControl);
				}
				else if (_itemControl is Menu)
				{
					((MenuItem) ((Menu) _itemControl).Items[0]).Items.Remove(item.ItemControl);
				}
			}
			else if (_itemType == BarItemType.ComboBox)
			{
				// Remove string item from combo box
				((ComboBox) _itemControl).Items.Remove(itemKey);
			}
		}

		#endregion

		#region Other public and internal elements

		internal Control ItemControl
		{
			get
			{
				return _itemControl;
			}
			set
			{
				_itemControl = value;
			}
		}

		#endregion

		#region Private elements

		private void buttonItem_Click(object sender, RoutedEventArgs e)
		{
			RaiseItemClickEvent();
		}

		private void newDropButton_Click(object sender, RoutedEventArgs e)
		{
			RaiseItemClickEvent();
		}

		private void newMenuItem_Click(object sender, RoutedEventArgs e)
		{
			RaiseItemClickEvent();
		}

		private void RaiseItemClickEvent()
		{
			// Notify bar service about click to raise a global event
			_parentBarServ.HandleItemClick(this);
		}

		private UIElement CreateButtonInnerContent()
		{
			Grid innerGrid = new Grid();

			// Create grid column defintions
			innerGrid.ColumnDefinitions.Add(new ColumnDefinition()
			{
				Width = GridLength.Auto
			});
			innerGrid.ColumnDefinitions.Add(new ColumnDefinition()
			{
				Width = GridLength.Auto
			});

			// Initialize Image and TextBlock controls
			_buttonImage = new Image();
			_buttonImage.Width = 16;
			_buttonImage.Height = 16;

			_buttonTextBlock = new TextBlock();
			_buttonTextBlock.VerticalAlignment = VerticalAlignment.Center;
			_buttonTextBlock.Height = 16;

			Thickness padding = _buttonTextBlock.Padding;
			padding.Left = 2;
			_buttonTextBlock.Padding = padding;

			// Add the controls to grid
			innerGrid.Children.Add(_buttonImage);
			Grid.SetColumn(_buttonImage, 0);
			innerGrid.Children.Add(_buttonTextBlock);
			Grid.SetColumn(_buttonTextBlock, 1);

			// Return the grid
			return innerGrid;
		}

		private UIElement CreateDropDownButtonHeader()
		{
			Grid innerGrid = new Grid();

			// Create grid column defintions
			innerGrid.ColumnDefinitions.Add(new ColumnDefinition()
			{
				Width = GridLength.Auto
			});
			innerGrid.ColumnDefinitions.Add(new ColumnDefinition()
			{
				Width = GridLength.Auto
			});
			innerGrid.ColumnDefinitions.Add(new ColumnDefinition()
			{
				Width = GridLength.Auto
			});

			// Initialize Image
			_buttonImage = new Image();
			_buttonImage.Width = 16;
			_buttonImage.Height = 16;

			// Initialize TextBlock controls
			_buttonTextBlock = new TextBlock();
			Thickness padding = _buttonTextBlock.Padding;
			padding.Left = 2;
			padding.Bottom = 2;
			_buttonTextBlock.Padding = padding;
			_buttonTextBlock.VerticalAlignment = VerticalAlignment.Center;

			TextBlock arrowTextBlock = new TextBlock();
			arrowTextBlock.FontFamily = new FontFamily("Marlett");
			arrowTextBlock.FontSize = 9;
			arrowTextBlock.Text = "6";
			arrowTextBlock.VerticalAlignment = VerticalAlignment.Center;

			padding = arrowTextBlock.Padding;
			padding.Left = 2;
			arrowTextBlock.Padding = padding;

			// Add the controls to grid
			innerGrid.Children.Add(_buttonImage);
			Grid.SetColumn(_buttonImage, 0);
			innerGrid.Children.Add(_buttonTextBlock);
			Grid.SetColumn(_buttonTextBlock, 1);
			innerGrid.Children.Add(arrowTextBlock);
			Grid.SetColumn(arrowTextBlock, 2);

			// Return the grid
			return innerGrid;
		}

		private string FilterShortcutChars(string text)
		{
			if (!String.IsNullOrEmpty(text))
			{
				// Remove underscores from the text and convert double underscores to a single one
				return text.Replace("\xFE\xFE", "_").Replace("_", "").Replace("__", "\xFE\xFE");
			}
			else
			{
				return text;
			}
		}

		#endregion

	}

}
